home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’96 / Internet Chooser / reggie / basic / stddebug.c < prev    next >
Text File  |  1996-06-22  |  5KB  |  154 lines

  1. /* File "stddebug.c", Light Sockets - Copyright (C) Matt Slot, 1996           */
  2. /* Standard debugging and error tracking macros.                              */
  3.  
  4. /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
  5. /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
  6. /* Include Files */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <time.h>
  12. #include <unistd.h>
  13.  
  14. #if defined(PLATFORM_MAC)
  15. #include <LowMem.h>
  16. #include <MixedMode.h>
  17. #include <Notification.h>
  18. #endif
  19.  
  20. #include "stddebug.h"
  21.  
  22. /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
  23. /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
  24.  
  25. #define EOL        "\r\n"
  26. #define MSG        ((fatal) ? "Assert" : ((thrown) ? "Throw" : "Trace"))
  27.  
  28. /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
  29. /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
  30.  
  31. void _DebugPrintErr(char *msg, long err, char *file, char *line, 
  32.         int thrown, int fatal) {
  33.     if (!msg || !*msg) msg = MSG;
  34.     if (!err) fprintf(stderr, "%s @ %s:%s" EOL, msg, file, line);
  35.       else fprintf(stderr, "%s : %ld @ %s:%s" EOL, msg, err, file, line);
  36.     if (fatal) exit((err) ? err : 1);
  37.     }
  38.  
  39. /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
  40. /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
  41.  
  42. void _DebugPrintLog(char *msg, long err, char *file, char *line, 
  43.         int thrown, int fatal) {
  44.     static int inited = 0;
  45.     char *logname = "logfile.txt";
  46.     FILE *logfile = 0;
  47.     char time_str[80];
  48.     struct tm *time_tm;
  49.     time_t time_now;
  50.     
  51.     if (logfile = fopen(logname, (!inited) ? "w" : "a")) {
  52.         if (!inited) {
  53.             time_now = time(0);
  54.             time_tm = localtime(&time_now);
  55.             strftime(time_str, sizeof(time_str), "%c", time_tm);
  56.             fprintf(logfile, "Debug logfile dated: %s" EOL, time_str);
  57.             fprintf(logfile, "Binary dated: " __DATE__ ", " __TIME__ EOL);
  58.             fprintf(logfile, EOL);
  59.             inited = 1;
  60.             }
  61.     
  62.         if (!msg || !*msg) msg = MSG;
  63.         if (!err) fprintf(logfile, "%s @ %s:%s" EOL, msg, file, line);
  64.           else fprintf(logfile, "%s : %ld @ %s:%s" EOL, msg, err, file, line);
  65.         fclose(logfile);
  66.         }
  67.     if (fatal) exit((err) ? err : 1);
  68.     }
  69.  
  70. /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
  71. /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
  72.  
  73. #if defined(PLATFORM_MAC)
  74. void _DebugMacsbug(char *msg, long err, char *file, char *line, 
  75.         int thrown, int fatal) {
  76.     unsigned char debugstr[257];
  77.     
  78.     if (!msg || !*msg) msg = MSG;
  79.     sprintf((char*)debugstr+1, "%s : %ld @ %s:%s%s", msg, err, file, line,
  80.             (fatal || thrown) ? "" : ";g");
  81.     debugstr[0] = strlen((char*)debugstr+1);
  82.     
  83.     DebugStr(debugstr);
  84.     if (fatal) ExitToShell();
  85.     }
  86.  
  87. /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
  88. /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
  89.  
  90. static pascal void _DebugNotifyResp(NMRecPtr nmPtr) {
  91.     Boolean *busy = (Boolean *) nmPtr->nmRefCon;
  92.     *busy = false;
  93.     }
  94.  
  95. /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
  96. /* **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** */
  97.  
  98. void _DebugNotify(char *msg, long err, char *file, char *line, 
  99.         int thrown, int fatal) {
  100.     const short kMaxNotifications = 10;
  101.     static Boolean nmBusy[kMaxNotifications] = {0,0,0,0,0,0,0,0,0,0};
  102.     static NMRec nmRec[kMaxNotifications];
  103.     static Str255 nmStr[kMaxNotifications];
  104. #if GENERATINGCFM
  105.     RoutineDescriptor nmRD = 
  106.             BUILD_ROUTINE_DESCRIPTOR(uppNMProcInfo, _DebugNotifyResp);
  107.     NMUPP nmUPP = &nmRD;
  108. #else
  109.     NMUPP nmUPP = _DebugNotifyResp;
  110. #endif
  111.     long i, j, k;
  112.     
  113.     for(i=0; i<kMaxNotifications && nmBusy[i]; i++);
  114.     if (i==kMaxNotifications) return;
  115.     
  116.     nmBusy[i] = true;
  117.     nmRec[i].qType = nmType;
  118.     nmRec[i].nmMark = 0;
  119.     nmRec[i].nmIcon = 0;
  120.     nmRec[i].nmSound = (Handle) -1;
  121.     nmRec[i].nmStr = nmStr[i];
  122.     nmRec[i].nmResp = nmUPP;
  123.     nmRec[i].nmRefCon = (long) &(nmBusy[i]);
  124.     
  125. #if GENERATINGCFM
  126.     MakeDataExecutable(&nmRD, sizeof(nmRD));
  127. #endif
  128.  
  129.     if (!msg || !*msg) msg = MSG;
  130.  
  131. #define _pstrcatcstr(cs,ps) \
  132.         { BlockMoveData((cs), (ps)+(ps)[0]+1, strlen(cs)); (ps)[0]+=strlen(cs); }
  133. #define _pstrcatchar(ch,ps)    { (ps)[++(ps)[0]] = (ch); }
  134.  
  135.     nmStr[i][0] = 0;
  136.     _pstrcatcstr(msg, nmStr[i]);
  137.     _pstrcatchar(' ', nmStr[i]);
  138.     if (err) {
  139.         _pstrcatchar('(', nmStr[i]);
  140.         if (err < 0) { _pstrcatchar('-', nmStr[i]); err = 0 - err; }
  141.         for(j=8, k=100000000; j>=0; j--,k/=10) 
  142.             if ((err/k)%10 || !j) _pstrcatchar('0' + (err/k)%10, nmStr[i]);
  143.         _pstrcatchar(')', nmStr[i]);
  144.         }
  145.     _pstrcatcstr("\rFile \"", nmStr[i]);
  146.     _pstrcatcstr(file, nmStr[i]);
  147.     _pstrcatcstr("\" at line ", nmStr[i]);
  148.     _pstrcatcstr(line, nmStr[i]);
  149.  
  150.     NMInstall(&(nmRec[i]));
  151.     }
  152.  
  153. #endif
  154.